package com.shop.controller;

import com.shop.pojo.pms.Pms_Product_Attr_Value;
import com.shop.pojo.pms.Pms_Sku_Images;
import com.shop.pojo.pms.Pms_Sku_Info;
import com.shop.pojo.pms.Pms_Spu_Info;
import com.shop.service.Pms_Product_Attr_Value_Service;
import com.shop.service.Pms_SkuInfo_Service;
import com.shop.service.Pms_Sku_Images_Service;
import com.shop.service.Pms_Spu_Info_Service;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import com.shop.vo.SkuItemVo;
import com.shop.vo.addspuvo.SpuSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
@Autowired
private Pms_SkuInfo_Service pms_skuInfo_service;
@Autowired
private Pms_Spu_Info_Service pms_spu_info_service;
@Autowired
private Pms_Product_Attr_Value_Service pms_product_attr_value_service;
@Autowired
private Pms_Sku_Images_Service pms_sku_images_service;
    //查询商品详情
@GetMapping("/skuItem/{skuId}")
    public SkuItemVo item(@PathVariable("skuId") Long skuId) {
             System.out.println(pms_skuInfo_service.item(skuId));
        return pms_skuInfo_service.item(skuId);
    }
//多条件分页查询全部spu信息
    @GetMapping("/findAllSpu")
    public LayuiUtils<Pms_Spu_Info> pms_spu_infoLayuiUtils(Pms_Spu_Info pms_spu_info,Integer page){

    return pms_spu_info_service.PMS_SPU_INFO_LAYUI_UTILS(pms_spu_info, page);
    }
    //查询spu下的全部分类和品牌
    @GetMapping("/categoryandbrand")
    public R CATEGORY_AND_BRAND_BY_SPU_ID(){

    return pms_spu_info_service.CATEGORY_AND_BRAND_BY_SPU_ID();
    }
    //查询spu下面全部的属性和分组和属性值
    @GetMapping("/findproductAttrValuesBySpuId")
    public R findproductAttrValuesBySpuId(Long spuId){

    return pms_product_attr_value_service.findGrouopByAttrByValues(spuId);
    }
    //多条件分页查询sku信息
    @GetMapping("/findAllSku")
    public LayuiUtils<Pms_Sku_Info> skuInfoLayuiUtils(Pms_Sku_Info pms_sku_info,Integer page,String betWeenPrice){

    return pms_skuInfo_service.findAllSkuQuery(page, pms_sku_info, betWeenPrice);
    }
    //根据skuid来查询sku下的全部图片
    @GetMapping("/findAllSkuImg/{skuId}")
    public Object findAllSkuImageBySkuId(@PathVariable("skuId") Long skuId){

   return  pms_sku_images_service.findAllSkuImgById(skuId);
    }
    @GetMapping("/findSkuInfoBySkuId/{skuId}")
    public R findSkuInfoBySkuId(@PathVariable("skuId") Long skuId){

    return pms_skuInfo_service.findAllSkuInfoBySkuId(skuId);
    }

//添加spu信息
    @PostMapping("/savespu")
    public R saveSpu(@RequestBody SpuSaveVo spuSaveVo)  {

    System.out.println(spuSaveVo);
        pms_spu_info_service.saveSpuInfo(spuSaveVo);
    return R.success();
    }

    //修改商品上下架状态
    @PutMapping("/putSpuByStatus")
    public R putSpuByStatus(Pms_Spu_Info pms_spu_info){
System.out.println(pms_spu_info.getId()+" ------------"+pms_spu_info.getPublish_status());
    return pms_spu_info_service.exitSpuByStatus(pms_spu_info);
    }
    /*
     * @Description 批量修改商品的属性值
     * @param pms_product_attr_value
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/11/9 11:14
     */
    @PutMapping("/putProductByValues")
    public R putProductByValues(@RequestBody List<Pms_Product_Attr_Value> pms_product_attr_value){


        return pms_product_attr_value_service.updateAttrByValues(pms_product_attr_value);

    }
    //先删除sku下的图片，然后在批量添加
    @PostMapping("/addOrdelSkuImage")
    public R putProductBySkuImage(@RequestBody List<Pms_Sku_Images> pms_sku_images){

        return pms_sku_images_service.exitSkuImages(pms_sku_images);
    }

}
